<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.order_pay === '0'"
              >未付款</el-tag
            >
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="showEditOrder(scope.row.order_id)"
            ></el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-location"
              @click="showlogistics"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="修改订单"
      :visible.sync="editOrderDialogVisible"
      width="50%"
      @close="editOrderClose"
    >
      <el-form
        :model="orderForm"
        :rules="orderFormrules"
        ref="orderFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="订单编号" prop="order_price">
          <el-input v-model="orderForm.order_number" disabled></el-input>
        </el-form-item>
        <el-form-item label="订单价格" prop="order_price">
          <el-input v-model="orderForm.order_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="发货状态" prop="is_send">
          <el-select v-model="orderForm.is_send" placeholder="请选择发货状态">
            <el-option
              v-for="item in sendOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="order_pay">
          <el-select v-model="orderForm.order_pay" placeholder="请选择支付方式">
            <el-option
              v-for="item in payOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付状态" prop="pay_status">
          <el-select
            v-model="orderForm.pay_status"
            placeholder="请选择支付状态"
          >
            <el-option
              v-for="item in payStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            expand-trigger="hover"
            :options="citydata"
            v-model="orderForm.city"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailSite">
          <el-input v-model="orderForm.detailSite"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editOrderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrder">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="查看物流信息"
      :visible.sync="logiticsDialogVisible"
      width="50%"
    >
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="logiticsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="logiticsDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import citydata from './citydata'
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orderList: [],
      total: 0,
      editOrderDialogVisible: false,
      orderForm: {
        city: [],
        detailSite: ''
      },

      orderFormrules: {
        order_price: [
          { required: true, message: '请输入订单价格', trigger: 'blur' }
        ],
        order_number: [
          { required: true, message: '请输入订单数量', trigger: 'blur' }
        ],
        order_pay: [
          { required: true, message: '请选择支付方式', trigger: 'blur' }
        ],
        is_send: [
          { required: true, message: '请选择发货状态', trigger: 'blur' }
        ],
        pay_status: [
          { required: true, message: '请选择支付状态', trigger: 'blur' }
        ],
        city: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        detailSite: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ]
      },
      sendOptions: [
        { value: '0', label: '未发货' },
        { value: '1', label: '已经发货' }
      ],
      payOptions: [
        { value: '0', label: '未支付' },
        { value: '1', label: '支付宝' },
        { value: '2', label: '微信' },
        { value: '3', label: '银行卡' }
      ],
      payStatus: [
        { value: '0', label: '未付款' },
        { value: '1', label: '已付款' }
      ],
      citydata,
      logiticsDialogVisible: false,
      activities: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取订单数据列表失败!')
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (newsize) {
      this.queryInfo.pagesize = newsize
      this.getOrderList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    async showEditOrder (roderId) {
      const { data: res } = await this.$http.get('orders/' + roderId)
      if (res.meta.status !== 200) return this.$message.error('查看订单详情失败!')
      console.log(res)
      this.orderForm = res.data
      this.editOrderDialogVisible = true
    },
    editOrderClose () {
      this.$refs.orderFormRef.resetFields()
    },
    editOrder () {
      this.$refs.orderFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('orders/' + this.orderForm.order_id, this.orderForm)
        console.log(res)
        if (res.meta.status !== 201) return this.$message.error('修改订单失败!')
        this.$message.success('修改订单成功!')
        this.getOrderList()
        this.editOrderDialogVisible = false
      })
    },
    async showlogistics () {
      const { data: res } = await this.$http.get('kuaidi/')
      console.log(res)
      this.logiticsDialogVisible = true
    }
  }
}
</script>
<style scoped>
.el-select,
.el-cascader {
  width: 100%;
}
</style>
